# 生成自签名证书的 PowerShell 脚本
Write-Host "正在生成自签名 SSL 证书..." -ForegroundColor Green

# 检查是否安装了 OpenSSL
try {
    $null = openssl version
    Write-Host "检测到 OpenSSL，使用 OpenSSL 生成证书" -ForegroundColor Yellow
    
    # 使用 OpenSSL 生成私钥和证书
    openssl req -nodes -new -x509 -keyout server.key -out server.cert -days 365 -subj "/C=CN/ST=Beijing/L=Beijing/O=IFlyTek/OU=IT/CN=localhost"
    
    Write-Host "证书生成完成！" -ForegroundColor Green
    Write-Host "生成的文件：" -ForegroundColor Cyan
    Write-Host "  - server.key (私钥)" -ForegroundColor Cyan
    Write-Host "  - server.cert (证书)" -ForegroundColor Cyan
    
} catch {
    Write-Host "未检测到 OpenSSL，将使用 Node.js 内置方法生成证书" -ForegroundColor Yellow
    Write-Host "如需使用 OpenSSL，请先安装 OpenSSL" -ForegroundColor Yellow
    Write-Host "下载地址: https://slproweb.com/products/Win32OpenSSL.html" -ForegroundColor Blue
}

Write-Host "`n使用方法：" -ForegroundColor Green
Write-Host "1. 运行: npm run https" -ForegroundColor White
Write-Host "2. 访问: https://localhost:4430" -ForegroundColor White
Write-Host "3. 浏览器会提示安全警告，选择'继续访问'即可" -ForegroundColor Yellow
